﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace FlashCards.Tests.NeuralNetworkTest.Simulations
{
    public class FlashcardsManager
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="flashcardsCount"></param>
        /// <param name="userId"></param>
        /// <returns>ID of created set</returns>
        public static int CreateFlashcardSet(int flashcardsCount, int userId, string name)
        {
            int id = 0;
            using (var context = new Model.FlashCardsEntities())
            {
                var set = new Model.FlashcardSet()
                {
                    CreatorId = userId,
                    CategoryId = context.Categories.FirstOrDefault().CategoryId,
                    DateModified = DateTime.Now,
                    IsPublic = true,
                    Name = name,
                    Tags = "tags"
                };

                for (int i = 0; i < flashcardsCount; i++)
                {
                    set.Flashcards.Add(new Model.Flashcard
                        {
                            DateModified = DateTime.Now,
                            Definition = new Model.FlashcardPart { Data = Encoding.UTF8.GetBytes("Definition" + i), TypeId = 1, DateModified = DateTime.Now },
                            Term = new Model.FlashcardPart { Data = Encoding.UTF8.GetBytes("Term" + i), TypeId = 1, DateModified = DateTime.Now }
                        });
                }

                context.FlashcardSets.AddObject(set);

                context.SaveChanges();
                id = set.FlashcardSetId;
            }
            return id;
        }
    }
}
